package com.studyv3.test.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.studyv3.common.core.page.PageQuery;
import com.studyv3.common.core.page.TableDataInfoPlus;
import com.studyv3.common.utils.StringUtils;
import com.studyv3.test.domain.StudyBookTypes;
import com.studyv3.test.domain.StudyUserBook;
import com.studyv3.test.domain.bo.StudyBookTypesBo;
import com.studyv3.test.domain.bo.StudyUserBookBo;
import com.studyv3.test.domain.vo.StudyBookTypesVo;
import com.studyv3.test.domain.vo.StudyUserBookVo;
import com.studyv3.test.mapper.StudyBookTypesMapper;
import com.studyv3.test.mapper.StudyUserBookMapper;
import com.studyv3.test.service.IStudyBookTypesService;
import com.studyv3.test.service.IStudyUserBookService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * (StudyBookTypes)表服务实现类
 *
 * @author makejava
 * @since 2024-04-25 16:00:59
 */
@Service
@RequiredArgsConstructor
public class StudyUserBookServiceImpl extends ServiceImpl<StudyUserBookMapper, StudyUserBook> implements IStudyUserBookService {
    private final StudyUserBookMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public StudyUserBookVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfoPlus<StudyUserBookVo> queryPageList(StudyUserBookBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<StudyUserBook> lqw = buildQueryWrapper(bo);
        int total = Math.toIntExact(count(lqw));

        int page = (pageQuery.getPageNum()-1)*pageQuery.getPageSize();
        lqw.last("limit " + page + "," + pageQuery.getPageSize());
        List<StudyUserBookVo> result = baseMapper.selectVoList(lqw);

        TableDataInfoPlus<StudyUserBookVo> build = TableDataInfoPlus.build(result);
        build.setTotal(total);
        return  build;
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<StudyUserBookVo> queryList(StudyUserBookBo bo) {
        LambdaQueryWrapper<StudyUserBook> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<StudyUserBook> buildQueryWrapper(StudyUserBookBo bo) {
        LambdaQueryWrapper<StudyUserBook> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, StudyUserBook::getUserId, bo.getUserId());
        lqw.eq(bo.getBookId() != null, StudyUserBook::getBookId, bo.getBookId());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(StudyUserBookBo bo) {
        StudyUserBook add = BeanUtil.toBean(bo, StudyUserBook.class);
//        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(StudyUserBookBo bo) {
        StudyUserBook update = BeanUtil.toBean(bo, StudyUserBook.class);
//        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(StudyUserBook entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}

